<?php


class BaseController extends spController{
	
	public $Conf = null;
	public $WX = null;
	
	function __construct(){
		parent::__construct();
		include_once '/config/config.php' ;
		global $SiteConfig;
		$this->Conf = $SiteConfig;		
		$this->_InitBaseUrl();
		include_once './'.APP_NAME.'/include/Wechat.php';
		$this->WX = new Wechat($GLOBALS['G_SP']['wechat']);
	}
	
	
	function _InitBaseUrl(){
		
		define('B_Url',$this->Conf['Site']['B_Url']);
		
		//项目根目录
		$bUrl = B_Url;
		
		define('IMG_Url',B_Url.'public/image/');
		define('JS_Url',B_Url.'public/js/');
		define('CSS_Url',B_Url.'public/css/');
		define('UPLOAD_Url',B_Url.'public/upload/');
		
		define('SOURCE_Url',B_Url.'public/assets/');
		
		define('APP_BASE_Url',B_Url.APP_NAME.'/www/');
		define('APP_CSS_Url',B_Url.APP_NAME.'/www/css/');
		define('APP_IMG_Url',B_Url.APP_NAME.'/www/image/');
		define('APP_JS_Url',B_Url.APP_NAME.'/www/js/');
		define('APP_UPLOAD_Url',B_Url.APP_NAME.'/www/upload/');
		
		$this->B_Url = $bUrl;
		$this->IMG_Url = IMG_Url;
		$this->JS_Url = JS_Url;
		$this->CSS_Url = CSS_Url;
		$this->UPLOAD_Url = UPLOAD_Url;
		
		$this->APP_BASE_Url = APP_BASE_Url; 
		$this->APP_CSS_Url = APP_CSS_Url; 
		$this->APP_IMG_Url = APP_IMG_Url; 
		$this->APP_JS_Url = APP_JS_Url; 
		$this->APP_UPLOAD_Url = APP_UPLOAD_Url; 
		
		$this->SOURCE_Url = B_Url.'public/assets/'; 
	}
	
	
	/**
	* 打印调试信息
	* @param 被调试的数据 $obj
	* @param 是否结果执行 默认会结束执行 $IS_END
	* 
	* @return
	*/
	public function _D($obj,$IS_END=true){
		echo '<pre>';
		if(is_array($obj)){
			print_r($obj);
		}elseif(is_string($obj)){
			echo $obj;
		}elseif(is_object($obj)){
			var_dump($obj);
		}elseif(empty($obj)){
			echo 'IS_NULL';
		}else{
			var_dump($obj);
		}
		echo '</pre>';
		if($IS_END){
			exit();
		}
	}
	
	/**
	* 返回Json字符串
	* @param 状态标志 默认为TRUE $status
	* @param 信息 $mes
	* @param 数据 $data 
	* @return
	*/
	public function _Ajax_return($status=true,$mes='Done',$data=array()){
		header("Access-Control-Allow-Origin:*");
		header("Access-Control-Allow-Headers:X-Requested-With");
		//header('content-type:application/json;charset=utf8');
		$result = array('s'=>$status,'m'=>$mes,'d'=>$data);
		/*
		
		echo '<pre>';
		print_r($result);
		exit();
		*/
		exit(json_encode($result));
	}
	
	/**
	* 记录日志文件
	* @param 被记录的数据 $data
	* @param 日志文件名称 默认为日期 $filename
	* @param 日志记录行的标记 $mark
	* 
	* @return
	*/
	public function _log($data,$filename='',$mark=''){
		$logfile = Log_Path;
		if(!empty($filename)){
			$logfile .= $filename.'.log';
		}else{
			$logfile .= date('Y_m_d_H').'.log';
		}
		$file = fopen($logfile,'a+');
		if(is_string($data)){
			$dataStr = '【Sting】'.$data;
		}elseif(is_array($data)){
			$dataStr = '【Array】'.json_encode($data,JSON_UNESCAPED_UNICODE);
		}elseif(is_object($data)){
			$dataStr = '【Object】'.json_encode($this->_object_to_array($data));
		}else{
			$dataStr = '【Unknow Type】'.$data;
		}
		$dataStr = date('Ymd_His').$dataStr.'
';
		if(!empty($mark)){
			$dataStr = '【'.$mark.'】'.$dataStr;
		}else{
			$dataStr = '【----】'.$dataStr;
		}
		fwrite($file,$dataStr);
		fclose(); 
	}
	
	/**
	* 对象转化为Array
	* @param undefined $obj
	* 
	* @return
	*/
	public function _object_to_array($obj){
	    $_array = is_object($obj) ? get_object_vars($obj) : $obj;
	    foreach ($_array as $key => $v) {
	        $v = (is_array($v) || is_object($v)) ? _object_to_array($v) : $v;
	        $array[$key] = $v;
	    }
	    return $array;
	} 
	
}
?>